<template>
  <div class="login-container">
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        type="number"
        placeholder=" 请输入手机号"
        maxlength="11"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        type="number"
        placeholder="请输入验证码"
        maxlength="6"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="isCountDownShow"
            :time="10000"
            format="ss s"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            size="small"
            native-type="button"
            @click="onSendSms"
            >获取验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from "@/api/user";
export default {
  name: "LoginIndex",
  data() {
    return {
      user: {
        mobile: "13911111111",
        code: "246810",
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空",
          },
          {
            pattern: /^1[35678]\d{9}$/,
            message: "手机号格式错误",
          },
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空",
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误",
          },
        ],
      },
      isCountDownShow: false,
    };
  },
  methods: {
    async onSubmit() {
      const user = this.user;
      this.$toast.loading({
        duration: 0,
        message: "登录中",
        forbidClick: true,
      });
      try {
        const res = await login(user);
        console.log(res);
        this.$store.commit("setUser", res.data.data);
        // 登录成功，跳转回原来页面
        // back 的方式不严谨:?
        this.$router.back()
        this.$toast.success({
          message: "登录成功",
          duration: 1000,
        });
      } catch (err) {
        console.log(err);
        if (err.response?.status === 400) {
          this.$toast.fail({
            message: "手机号或验证码错误",
            duration: 1000,
          });
        } else {
          this.$toast.fail("登录失败，请稍后重试");
        }
      }
    },
    async onSendSms() {
      // 校验手机号
      try {
        // validate返回的是Promise对象
        await this.$refs.loginForm.validate("mobile");
        console.log("验证通过");
      } catch (err) {
        // return 格式错误结束进程，下面代码不执行
        return console.log("手机格式错误", err);
      }
      // 验证通过显示倒计时
      this.isCountDownShow = true;
      // 请求发送验证码
      try {
        await sendSms(this.user.mobile);
        this.$toast.success("发送成功");
      } catch (err) {
        this.isCountDownShow = false;
        this.$toast.fail("发送失败");
        console.log(err);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .page-nav-bar {
    .van-icon, van-icon-cross {
      color: #fff;
    }
  }

  .send-sms-btn {
    padding: 0;
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 28px;
    .login-btn {
      background-color: #6db4fb;
      border-radius: 10px;
      border: none;
    }
  }
}
</style>>